package com.zhang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.client.SearchFeignClient;
import com.zhang.constant.MqConst;
import com.zhang.entity.*;
import com.zhang.result.RetVal;
import com.zhang.service.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author zbw
 * @since 2022-04-07
 */
@RestController
@RequestMapping("/product")
public class SkuController {
    @Autowired
    private ProductImageService productImageService;

    @Autowired
    private ProductSalePropertyKeyService salePropertyKeyService;
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private SearchFeignClient searchFeignClient;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //1.根据spu查询spu的图片
    //http://192.168.15.218/product/queryProductImageByProductId/12
    @GetMapping("queryProductImageByProductId/{spuId}")
    public RetVal queryProductImageByProductId(@PathVariable Long spuId){
        QueryWrapper<ProductImage> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",spuId);
        List<ProductImage> productImageList = productImageService.list(wrapper);
        return RetVal.ok(productImageList);
    }

    //2.根据spu查询spu的销售属性
    //http://192.168.15.218/product/querySalePropertyByProductId/12
    @GetMapping("querySalePropertyByProductId/{spuId}")
    public RetVal querySalePropertyByProductId(@PathVariable Long spuId){
        List<ProductSalePropertyKey> productSalePropertyKeyList=salePropertyKeyService.querySalePropertyByProductId(spuId);
        return RetVal.ok(productSalePropertyKeyList);
    }

    //3.保存商品sku
    //http://127.0.0.1/product/saveSkuInfo
    @PostMapping("saveSkuInfo")
    public RetVal saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuInfoService.saveSkuInfo(skuInfo);
        return RetVal.ok();
    }

    //4.商品SKU分页列表查询
    //http://192.168.15.218/product/querySkuInfoByPage/1/10
    @GetMapping("querySkuInfoByPage/{currentPageNum}/{pageSize}")
    public RetVal querySkuInfoByPage(@PathVariable Long currentPageNum,
                                     @PathVariable Long pageSize){
        Page<SkuInfo> page = new Page<>(currentPageNum,pageSize);
        skuInfoService.page(page,null);
        return RetVal.ok(page);
    }

    //5.商品skuInfo上架
    //http://127.0.0.1/product/onSale/26
    @GetMapping("onSale/{skuId}")
    public RetVal onSale(@PathVariable Long skuId){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        //（1：是 0：否）
        skuInfo.setIsSale(1);
        skuInfoService.updateById(skuInfo);
        // 后面涉及到搜索环节的时候再编写代码,替换为rabbitmq
        //searchFeignClient.onSale(skuId);
        rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE,MqConst.ON_SALE_ROUTING_KEY,skuId);
        return RetVal.ok();
    }

    //6.商品skuInfo下架
    //http://127.0.0.1/product/offSale/26
    @GetMapping("offSale/{skuId}")
    public RetVal offSale(@PathVariable Long skuId){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        //（1：是 0：否）
        skuInfo.setIsSale(0);
        skuInfoService.updateById(skuInfo);
        // 后面涉及到搜索环节的时候再编写代码
        //searchFeignClient.offSale(skuId);
        rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE,MqConst.OFF_SALE_ROUTING_KEY,skuId);
        return RetVal.ok();
    }
}

